/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.emex.app.stuff;

import com.emex.app.shared.data.Car;
import com.emex.app.shared.data.Client;
import com.emex.app.shared.data.OrderPayment;
import com.emex.app.shared.data.OrderPosition;
import com.emex.app.shared.data.Orders;
import com.emex.app.shared.data.User;
import com.emex.app.stuff.constant.ReportName;
import com.emex.app.stuff.gui.SelectClientDialog;
import com.emex.app.stuff.gui.CarDialog;
import com.emex.app.stuff.gui.ClientDialog;
import com.emex.app.stuff.gui.OrderDialog;
import com.emex.app.stuff.gui.OrderPaymentDialog;
import com.emex.app.stuff.gui.model.CarComboBoxModel;
import com.emex.app.stuff.gui.model.OrderPaymentTableModel;
import com.emex.app.stuff.gui.model.OrdersTableModel;
import com.emex.app.stuff.provider.AuthenticationDataProvider;
import com.emex.app.stuff.security.SecurityManager;
import com.emex.app.stuff.provider.CarDataProvider;
import com.emex.app.stuff.provider.ClientDataProvider;
import com.emex.app.stuff.provider.OrderDataProvider;
import com.emex.app.stuff.utils.DateUtils;
import com.emex.app.stuff.utils.MoneyToString;
import com.emex.app.stuff.utils.ReportGenerator;
import com.emex.app.stuff.utils.UIUtils;
import java.awt.Frame;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRMapArrayDataSource;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//com.emex.app.stuff//StaffInfo//EN",
        autostore = false
)
@TopComponent.Description(
        preferredID = "StaffInfoTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_NEVER
)
@TopComponent.Registration(mode = "editor", openAtStartup = true)
@ActionID(category = "Window", id = "com.emex.app.stuff.StaffInfoTopComponent")
@ActionReference(path = "Menu/Клиенты" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_StaffInfoAction",
        preferredID = "StaffInfoTopComponent"
)
@Messages({
    "CTL_StaffInfoAction=Информация о заказах",
    "CTL_StaffInfoTopComponent=Информация о заказах",
    "HINT_StaffInfoTopComponent=Информация о заказах клиента"
})
public final class StaffInfoTopComponent extends TopComponent {

    private static Frame mainWindow = WindowManager.getDefault().getMainWindow();

    private OrdersTableModel ordersTableModel;

    private OrderPaymentTableModel orderPaymentTableModel;

    /**
     * Current client.
     */
    private Client client;

    public StaffInfoTopComponent() {
        initComponents();
        setName(Bundle.CTL_StaffInfoTopComponent());
        setToolTipText(Bundle.HINT_StaffInfoTopComponent());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        staffNameLabel = new javax.swing.JLabel();
        searchPanel = new javax.swing.JPanel();
        searchTextField = new javax.swing.JTextField();
        searchButton = new javax.swing.JButton();
        addNewClientButton = new javax.swing.JButton();
        buttonsPanel = new javax.swing.JPanel();
        addOrderButton = new javax.swing.JButton();
        editOrderButton = new javax.swing.JButton();
        deleteOrderButton = new javax.swing.JButton();
        printButton = new javax.swing.JButton();
        printOrderBillButton = new javax.swing.JButton();
        orderPositionSearchPanel = new javax.swing.JPanel();
        orderPositionNameField = new javax.swing.JTextField();
        searchOPButton = new javax.swing.JButton();
        staffInfoScrollPanel = new javax.swing.JScrollPane();
        staffInfoPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        surnameField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        nameField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        patronymicField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        phonesField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        carsComboBox = new javax.swing.JComboBox();
        addNewCar = new javax.swing.JButton();
        viewCarButton = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        emailField = new javax.swing.JTextField();
        isInternetUser = new javax.swing.JCheckBox();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        commentTextArea = new javax.swing.JTextArea();
        ordersTableScroll = new javax.swing.JScrollPane();
        ordersTable = new org.jdesktop.swingx.JXTable();

        org.openide.awt.Mnemonics.setLocalizedText(staffNameLabel, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.staffNameLabel.text")); // NOI18N

        searchPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.searchPanel.border.title"))); // NOI18N

        searchTextField.setText(org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.searchTextField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(searchButton, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.searchButton.text")); // NOI18N
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(addNewClientButton, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.addNewClientButton.text")); // NOI18N
        addNewClientButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNewClientButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout searchPanelLayout = new javax.swing.GroupLayout(searchPanel);
        searchPanel.setLayout(searchPanelLayout);
        searchPanelLayout.setHorizontalGroup(
            searchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(searchPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(searchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(searchTextField)
                    .addGroup(searchPanelLayout.createSequentialGroup()
                        .addComponent(searchButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(addNewClientButton)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        searchPanelLayout.setVerticalGroup(
            searchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(searchPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(searchTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(searchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchButton)
                    .addComponent(addNewClientButton))
                .addContainerGap())
        );

        buttonsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        addOrderButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/add.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(addOrderButton, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.addOrderButton.text")); // NOI18N
        addOrderButton.setEnabled(false);
        addOrderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addOrderButtonActionPerformed(evt);
            }
        });

        editOrderButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/edit.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(editOrderButton, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.editOrderButton.text")); // NOI18N
        editOrderButton.setEnabled(false);
        editOrderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editOrderButtonActionPerformed(evt);
            }
        });

        deleteOrderButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/delete.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(deleteOrderButton, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.deleteOrderButton.text")); // NOI18N
        deleteOrderButton.setEnabled(false);
        deleteOrderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteOrderButtonActionPerformed(evt);
            }
        });

        printButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/print.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(printButton, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.printButton.text")); // NOI18N
        printButton.setEnabled(false);
        printButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                printButtonActionPerformed(evt);
            }
        });

        printOrderBillButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/print.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(printOrderBillButton, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.printOrderBillButton.text")); // NOI18N
        printOrderBillButton.setEnabled(false);
        printOrderBillButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                printOrderBillButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout buttonsPanelLayout = new javax.swing.GroupLayout(buttonsPanel);
        buttonsPanel.setLayout(buttonsPanelLayout);
        buttonsPanelLayout.setHorizontalGroup(
            buttonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buttonsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(addOrderButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(editOrderButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(deleteOrderButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(printButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(printOrderBillButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        buttonsPanelLayout.setVerticalGroup(
            buttonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buttonsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(buttonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addOrderButton)
                    .addComponent(deleteOrderButton)
                    .addComponent(editOrderButton)
                    .addComponent(printButton)
                    .addComponent(printOrderBillButton))
                .addContainerGap())
        );

        orderPositionSearchPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.orderPositionSearchPanel.border.title"))); // NOI18N
        orderPositionSearchPanel.setToolTipText(org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.orderPositionSearchPanel.toolTipText")); // NOI18N

        orderPositionNameField.setText(org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.orderPositionNameField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(searchOPButton, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.searchOPButton.text")); // NOI18N
        searchOPButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchOPButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout orderPositionSearchPanelLayout = new javax.swing.GroupLayout(orderPositionSearchPanel);
        orderPositionSearchPanel.setLayout(orderPositionSearchPanelLayout);
        orderPositionSearchPanelLayout.setHorizontalGroup(
            orderPositionSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(orderPositionSearchPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(orderPositionNameField)
                .addContainerGap())
            .addGroup(orderPositionSearchPanelLayout.createSequentialGroup()
                .addGap(77, 77, 77)
                .addComponent(searchOPButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        orderPositionSearchPanelLayout.setVerticalGroup(
            orderPositionSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(orderPositionSearchPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(orderPositionNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(searchOPButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        staffInfoScrollPanel.setBorder(null);

        staffInfoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.staffInfoPanel.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.jLabel1.text")); // NOI18N

        surnameField.setEditable(false);
        surnameField.setText(org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.surnameField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.jLabel2.text")); // NOI18N

        nameField.setEditable(false);
        nameField.setText(org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.nameField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.jLabel3.text")); // NOI18N

        patronymicField.setEditable(false);
        patronymicField.setText(org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.patronymicField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.jLabel4.text")); // NOI18N

        phonesField.setEditable(false);
        phonesField.setText(org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.phonesField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.jLabel5.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(addNewCar, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.addNewCar.text")); // NOI18N
        addNewCar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNewCarActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(viewCarButton, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.viewCarButton.text")); // NOI18N
        viewCarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewCarButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.jLabel6.text")); // NOI18N

        emailField.setEditable(false);
        emailField.setText(org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.emailField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(isInternetUser, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.isInternetUser.text")); // NOI18N
        isInternetUser.setEnabled(false);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel7, org.openide.util.NbBundle.getMessage(StaffInfoTopComponent.class, "StaffInfoTopComponent.jLabel7.text")); // NOI18N

        commentTextArea.setColumns(20);
        commentTextArea.setRows(5);
        commentTextArea.setEnabled(false);
        jScrollPane2.setViewportView(commentTextArea);

        javax.swing.GroupLayout staffInfoPanelLayout = new javax.swing.GroupLayout(staffInfoPanel);
        staffInfoPanel.setLayout(staffInfoPanelLayout);
        staffInfoPanelLayout.setHorizontalGroup(
            staffInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(staffInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(staffInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(surnameField)
                    .addComponent(nameField)
                    .addComponent(patronymicField)
                    .addComponent(phonesField)
                    .addComponent(carsComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(emailField)
                    .addGroup(staffInfoPanelLayout.createSequentialGroup()
                        .addGroup(staffInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6)
                            .addGroup(staffInfoPanelLayout.createSequentialGroup()
                                .addComponent(addNewCar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(viewCarButton))
                            .addComponent(isInternetUser)
                            .addComponent(jLabel7)
                            .addComponent(jLabel5))
                        .addGap(0, 24, Short.MAX_VALUE))
                    .addComponent(jScrollPane2))
                .addContainerGap())
        );
        staffInfoPanelLayout.setVerticalGroup(
            staffInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(staffInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(surnameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(patronymicField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(phonesField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(emailField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(isInternetUser)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(carsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(staffInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addNewCar)
                    .addComponent(viewCarButton))
                .addContainerGap())
        );

        staffInfoScrollPanel.setViewportView(staffInfoPanel);

        ordersTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ordersTableMouseClicked(evt);
            }
        });
        ordersTableScroll.setViewportView(ordersTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(buttonsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(ordersTableScroll))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(searchPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(orderPositionSearchPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(staffInfoScrollPanel)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(staffNameLabel)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(staffNameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(searchPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(orderPositionSearchPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(staffInfoScrollPanel))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(ordersTableScroll)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        if (null != searchTextField.getText()
                && !searchTextField.getText().isEmpty()) {
            List<Client> foundClient = ClientDataProvider.getInstance().
                    selectByPhoneOrSurname(searchTextField.getText());

            if (foundClient.isEmpty()) {
                UIUtils.showInfoMessage("Клиентов с заданными данными нет в базе");
            } else if (foundClient.size() == 1) {
                this.client = foundClient.get(0);
            } else {
                // Clients more than 1 - show table to choose correct one
                SelectClientDialog dialog = new SelectClientDialog(mainWindow, true, foundClient);
                dialog.setLocationRelativeTo(null);
                dialog.setVisible(true);
                client = dialog.getClient();
            }

            clientChanged();
        }
    }//GEN-LAST:event_searchButtonActionPerformed

    private void addNewCarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addNewCarActionPerformed
        if (client == null || null == client.getId()) {
            UIUtils.showErrorMessage("Клиент должен быть выбран!");
            return;
        }
        CarDialog dialog = new CarDialog(mainWindow, true);
        dialog.setLocationRelativeTo(null);
        dialog.setClient(client);
        dialog.setVisible(true);
        carsComboBox.setModel(new CarComboBoxModel(client));
        carsComboBox.setSelectedIndex(carsComboBox.getItemCount() - 1);
    }//GEN-LAST:event_addNewCarActionPerformed

    private void viewCarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewCarButtonActionPerformed
        if (null != carsComboBox.getSelectedItem()) {
            Car selection = (Car) carsComboBox.getItemAt(carsComboBox.getSelectedIndex());
            CarDialog dialog = new CarDialog(mainWindow, true);
            dialog.setTitle("Просмотр/редактирование автомобиля");
            dialog.setCar(selection);
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
        }
    }//GEN-LAST:event_viewCarButtonActionPerformed

    private void addOrderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addOrderButtonActionPerformed
        if (client.getIsInternetUser()) {
            OrderPaymentDialog dialog = new OrderPaymentDialog(mainWindow, true);
            dialog.setTableModelToUpdate(orderPaymentTableModel);
            dialog.setClient(client);
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
        } else {
            if (carsComboBox.getSelectedItem() == null) {
                UIUtils.showErrorMessage("Выберите автомобиль клиента");
                return;
            }
            Car selectedCar = (Car) carsComboBox.getSelectedItem();
            OrderDialog dialog = new OrderDialog(mainWindow, true);
            dialog.setOrdersTableModel(ordersTableModel);
            dialog.setClient(client);
            dialog.setCar(selectedCar);
            dialog.setOrder(null);
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
        }

    }//GEN-LAST:event_addOrderButtonActionPerformed

    private void deleteOrderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteOrderButtonActionPerformed
        if (!SecurityManager.getDefault().getCurrentPerson().getIsAdmin()) {
            UIUtils.notifyOperationNotAllowed();
            return;
        }
        if (client.getIsInternetUser()) {
            OrderPayment op = getOrderPaymentSelection();
            orderPaymentTableModel.dataRemoved(op);
            OrderDataProvider.getInstance().deleteOrderPaymentByPrimaryKey(op.getId());
        } else {
            Orders selection = getOrderSelection();
            ordersTableModel.dataRemoved(selection);
            OrderDataProvider.getInstance().deleteOrderPrimaryKey(selection.getId());
        }
    }//GEN-LAST:event_deleteOrderButtonActionPerformed

    private void editOrderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editOrderButtonActionPerformed
        if (client.getIsInternetUser()) {
            OrderPaymentDialog dialog = new OrderPaymentDialog(mainWindow, true);
            dialog.setClient(client);
            dialog.setOrderPayment(getOrderPaymentSelection());
            dialog.setTableModelToUpdate(orderPaymentTableModel);
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
        } else {
            OrderDialog dialog = new OrderDialog(mainWindow, true);
            dialog.setTitle("Редактирование/Просмотр заказа");
            dialog.setOrdersTableModel(ordersTableModel);
            dialog.setClient(client);
            dialog.setOrder(getOrderSelection());
            dialog.setLocationRelativeTo(this);
            dialog.setVisible(true);
        }
    }//GEN-LAST:event_editOrderButtonActionPerformed

    private void printButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printButtonActionPerformed
        if (client.getIsInternetUser()) {
            OrderPayment selection = getOrderPaymentSelection();
            if (null != selection) {
                try {
                    HashMap parameters = new HashMap();
                    boolean avtozavod = true;
                    String img;
                    String tmr;
                    String template;
                    if (avtozavod) {
                        img = "logo_avtoz.jpg";
                        tmr = "body.jpg";
                        template = ReportName.INTERNET_ORDER_REPORT_AVTOZAVOD;
                    } else {
                        img = "emexLogo.jpg";
                        tmr = "emexTimer.jpg";
                        template = ReportName.INTERNET_ORDER_REPORT;
                    }
                    BufferedImage image = ImageIO.read(getClass().getClassLoader().getResource(img));
                    BufferedImage timer = ImageIO.read(getClass().getClassLoader().getResource(tmr));
                    parameters.put("logo", image);
                    parameters.put("emexTimer", timer);
                    parameters.put("cur_date", DateUtils.getExtendedFormattedString(new Date()));
                    parameters.put("clientFullName", client.getSurname() + " " + client.getName() + " " + client.getPatronymic());
                    parameters.put("phone", client.getPhones());
                    String summ = selection.getSumma().toString() + " р.";
                    summ += "(" + new MoneyToString().convert(selection.getSumma()) + ")";
                    parameters.put("summa", summ);
                    JasperPrint jp = ReportGenerator.getInstance().generateReport(parameters, template);
                    ReportGenerator.getInstance().showReport(jp);
                } catch (Exception ex) {
                    Exceptions.printStackTrace(ex);
                    UIUtils.showErrorMessage("Произошли ошибки при печати бланка оплаты");
                }
            }
        } else {
            Orders selection = getOrderSelection();
            if (null != selection) {
                try {
                    HashMap parameters = new HashMap();
                    boolean isAvtozavod = true;
                    String template;
                    if (isAvtozavod) {
                        BufferedImage image = ImageIO.read(getClass().getClassLoader().getResource("body.jpg"));
                        BufferedImage logo = ImageIO.read(getClass().getClassLoader().getResource("logo_avtoz.jpg"));
                        parameters.put("logo", logo);
                        parameters.put("logo_address", image);
                        template = ReportName.ORDER_REPORT_DUB_AVTOZ;
                    } else {
                        BufferedImage image = ImageIO.read(getClass().getClassLoader().getResource("emexLogo.jpg"));
                        parameters.put("logo", image);
                        template = ReportName.ORDER_REPORT_DUB;
                    }
                    parameters.put("orderDate", DateUtils.getExtendedFormattedString(
                            selection.getDate()));
                    parameters.put("clientFullFIO", client.getSurname() + " "
                            + client.getName() + " " + client.getPatronymic());
                    parameters.put("phone", client.getPhones());
                    Car car = CarDataProvider.getInstance().selectByPrimaryKey(selection.getCarId());
                    parameters.put("car", car.getBrand() + " " + car.getModel());
                    parameters.put("VIN", car.getVin());
                    parameters.put("car_year", car.getYear());

                    User manager = AuthenticationDataProvider.getInstance().selectUserByPrimaryKey(selection.getManagerId());
                    if (null == manager) {
                        manager = SecurityManager.getDefault().getCurrentPerson();
                    }
                    String managerFio = manager.getSurname() + " "
                            + manager.getName() + " "
                            + manager.getPatronymic();

                    parameters.put("managerFullFIO", managerFio);
                    List<Map<String, String>> tableData = new ArrayList<>();
                    List<OrderPosition> orderPositions = OrderDataProvider.getInstance()
                            .getOrderPositionByOrderId(selection.getId());
                    Double genSumma = 0.0;
                    for (OrderPosition op : orderPositions) {
                        Map<String, String> ordPosInfo = new HashMap<>();
                        ordPosInfo.put("orderPositionName", op.getName());
                        ordPosInfo.put("amount", op.getAmount().toString());
                        ordPosInfo.put("price", op.getPrice().toString());
                        Double result = op.getPrice() * op.getAmount();
                        genSumma += result;
                        ordPosInfo.put("summa", result.toString());
                        tableData.add(ordPosInfo);
                    }
                    List<OrderPayment> orderPayments = OrderDataProvider.getInstance()
                            .getOrderPaymentByOrderId(selection.getId());
                    Double paySum = 0.0;
                    for (OrderPayment op : orderPayments) {
                        paySum += op.getSumma();
                    }
                    parameters.put("genSumma", genSumma.toString());
                    parameters.put("prepay", paySum.toString());
                    Double ost = genSumma - paySum;
                    parameters.put("ostatok", ost.toString());
                    Map<String, String>[] tableDS = tableData.toArray(new HashMap[tableData.size()]);
                    JRMapArrayDataSource mapDS = new JRMapArrayDataSource(tableDS);
                    parameters.put("datasource", new JREmptyDataSource());
                    parameters.put("dataSource", mapDS);

                    List<Map<String, String>> tableData2 = new ArrayList<>();
                    for (OrderPosition op : orderPositions) {
                        Map<String, String> ordPosInfo = new HashMap<>();
                        ordPosInfo.put("orderPositionName2", op.getName());
                        ordPosInfo.put("amount2", op.getAmount().toString());
                        ordPosInfo.put("price2", op.getPrice().toString());
                        Double result = op.getPrice() * op.getAmount();
                        ordPosInfo.put("summa2", result.toString());
                        tableData2.add(ordPosInfo);
                    }
                    Map<String, String>[] tableDS2 = tableData2.toArray(new HashMap[tableData2.size()]);
                    JRMapArrayDataSource mapDS2 = new JRMapArrayDataSource(tableDS2);
                    parameters.put("dataSource2", mapDS2);
                    JasperPrint jp = ReportGenerator.getInstance().generateReport(parameters, template);
                    ReportGenerator.getInstance().showReport(jp);
                } catch (Exception ex) {
                    Exceptions.printStackTrace(ex);
                    UIUtils.showErrorMessage("Произошли ошибки при печати бланка заказа.");
                }
            }
        }
    }//GEN-LAST:event_printButtonActionPerformed

    private void addNewClientButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addNewClientButtonActionPerformed
        ClientDialog dialog = new ClientDialog(mainWindow, true);
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        this.client = dialog.getClient();
        clientChanged();
    }//GEN-LAST:event_addNewClientButtonActionPerformed

    private void searchOPButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchOPButtonActionPerformed
        if (null != orderPositionNameField.getText()
                && !orderPositionNameField.getText().isEmpty()) {
            List<Client> foundClient = ClientDataProvider.getInstance().
                    getAllClientsByOrderPositionName(orderPositionNameField.getText());

            if (foundClient.isEmpty()) {
                UIUtils.showInfoMessage("По заданному названию позиции ничего не найдено");
            } else if (foundClient.size() == 1) {
                this.client = foundClient.get(0);
            } else {
                // Clients more than 1 - show table to choose correct one
                SelectClientDialog dialog = new SelectClientDialog(mainWindow, true, foundClient);
                dialog.setLocationRelativeTo(null);
                dialog.setVisible(true);
                client = dialog.getClient();
            }

            clientChanged();
        }
    }//GEN-LAST:event_searchOPButtonActionPerformed

    private void printOrderBillButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printOrderBillButtonActionPerformed
        Orders selection = getOrderSelection();
        if (null != selection) {
            try {
                HashMap parameters = new HashMap();
                boolean isAvtoozavod = true;
                String img;
                String tmr;
                String template;
                if (isAvtoozavod) {
                    img = "logo_avtoz.jpg";
                    tmr = "body.jpg";
                    template = ReportName.ORDER_BILL_REPORT_AVTOZ;
                } else {
                    img = "emexLogo.jpg";
                    tmr = "emexTimer.png";
                    template = ReportName.ORDER_BILL_REPORT;
                }
                BufferedImage image = ImageIO.read(getClass().getClassLoader().getResource(img));
                BufferedImage timer = ImageIO.read(getClass().getClassLoader().getResource(tmr));
                parameters.put("logo", image);
                parameters.put("emexTimer", timer);
                parameters.put("cur_date", DateUtils.getExtendedFormattedString(
                        new Date()));
                List<Map<String, String>> tableData = new ArrayList<>();
                List<OrderPosition> orderPositions = OrderDataProvider.getInstance()
                        .getOrderPositionByOrderId(selection.getId());
                Double genSumma = 0.0;
                Integer i = 1;
                for (OrderPosition op : orderPositions) {
                    Map<String, String> ordPosInfo = new HashMap<>();
                    ordPosInfo.put("number", i.toString());
                    ordPosInfo.put("detail_name", op.getName());
                    ordPosInfo.put("amount", op.getAmount().toString());
                    ordPosInfo.put("price", op.getPrice().toString());
                    Double result = op.getPrice() * op.getAmount();
                    genSumma += result;
                    ordPosInfo.put("summa", result.toString());
                    tableData.add(ordPosInfo);
                    ++i;
                }
                parameters.put("genSumma", genSumma.toString());
                Map<String, String>[] tableDS = tableData.toArray(new HashMap[tableData.size()]);
                JRMapArrayDataSource mapDS = new JRMapArrayDataSource(tableDS);
                parameters.put("datasource", new JREmptyDataSource());
                parameters.put("dataSource", mapDS);
                JasperPrint jp = ReportGenerator.getInstance().generateReport(parameters, template);
                ReportGenerator.getInstance().showReport(jp);
            } catch (final Exception ex) {
                Exceptions.printStackTrace(ex);
                UIUtils.showErrorMessage("Произошли ошибки при печати товарного чека.");
            }
        }
    }//GEN-LAST:event_printOrderBillButtonActionPerformed

    private void ordersTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ordersTableMouseClicked
        if (evt.getClickCount() == 2) {
            editOrderButtonActionPerformed(null);
        }
    }//GEN-LAST:event_ordersTableMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addNewCar;
    private javax.swing.JButton addNewClientButton;
    private javax.swing.JButton addOrderButton;
    private javax.swing.JPanel buttonsPanel;
    private javax.swing.JComboBox carsComboBox;
    private javax.swing.JTextArea commentTextArea;
    private javax.swing.JButton deleteOrderButton;
    private javax.swing.JButton editOrderButton;
    private javax.swing.JTextField emailField;
    private javax.swing.JCheckBox isInternetUser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField nameField;
    private javax.swing.JTextField orderPositionNameField;
    private javax.swing.JPanel orderPositionSearchPanel;
    private org.jdesktop.swingx.JXTable ordersTable;
    private javax.swing.JScrollPane ordersTableScroll;
    private javax.swing.JTextField patronymicField;
    private javax.swing.JTextField phonesField;
    private javax.swing.JButton printButton;
    private javax.swing.JButton printOrderBillButton;
    private javax.swing.JButton searchButton;
    private javax.swing.JButton searchOPButton;
    private javax.swing.JPanel searchPanel;
    private javax.swing.JTextField searchTextField;
    private javax.swing.JPanel staffInfoPanel;
    private javax.swing.JScrollPane staffInfoScrollPanel;
    private javax.swing.JLabel staffNameLabel;
    private javax.swing.JTextField surnameField;
    private javax.swing.JButton viewCarButton;
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened() {
        searchPanel.getRootPane().setDefaultButton(searchButton);
        ordersTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                int row = ordersTable.getSelectedRow();
                if ((row >= 0) && (row < ordersTable.getRowCount())) {
                    editOrderButton.setEnabled(true);
                    deleteOrderButton.setEnabled(true);
                    printButton.setEnabled(true);
                    printOrderBillButton.setEnabled(true);

                } else {
                    editOrderButton.setEnabled(false);
                    deleteOrderButton.setEnabled(false);
                    printButton.setEnabled(false);
                    printOrderBillButton.setEnabled(false);
                }
            }
        });
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    private void clientChanged() {
        if (null != client) {
            addOrderButton.setEnabled(true);
            nameField.setText(client.getName());
            patronymicField.setText(client.getPatronymic());
            surnameField.setText(client.getSurname());
            phonesField.setText(client.getPhones());
            emailField.setText(client.getEmail());
            commentTextArea.setText(client.getComment());
            isInternetUser.setSelected(client.getIsInternetUser());
            carsComboBox.setModel(new CarComboBoxModel(client));
            String clientFio = client.getSurname() + " "
                    + client.getName().charAt(0) + ". "
                    + client.getPatronymic().charAt(0) + ".";
            if (client.getIsInternetUser()) {
                printOrderBillButton.setVisible(false);
                staffNameLabel.setText("Информация об оплатах: " + clientFio);
                addOrderButton.setText("Добавить оплату");
                printButton.setText("Печать бланка оплаты");
                List<OrderPayment> orderPayments = OrderDataProvider.getInstance().getOrderPaymentByClientId(client.getId());
                orderPaymentTableModel = new OrderPaymentTableModel(orderPayments);
                ordersTable.setModel(orderPaymentTableModel);
            } else {
                printOrderBillButton.setVisible(true);
                staffNameLabel.setText("Информация о заказах: " + clientFio);
                addOrderButton.setText("Добавить заказ");
                printButton.setText("Печать бланка заказа");
                List<Orders> orders = OrderDataProvider.getInstance().getAllClientOrders(client.getId());
                ordersTableModel = new OrdersTableModel(orders);
                ordersTable.setModel(ordersTableModel);
            }
        } else if (null == client) {
            addOrderButton.setEnabled(false);
        }
    }

    protected Orders getOrderSelection() {
        if (ordersTable.getSelectedRow() > -1) {
            Orders orders = (Orders) ordersTable
                    .getValueAt(ordersTable.getSelectedRow(), -1);
            return orders;
        }
        return null;
    }

    protected OrderPayment getOrderPaymentSelection() {
        if (ordersTable.getSelectedRow() > -1) {
            OrderPayment op = (OrderPayment) ordersTable
                    .getValueAt(ordersTable.getSelectedRow(), -1);
            return op;
        }
        return null;
    }
}
